package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /*
    员工登录
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
       //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //数据库查询用户名
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        //获取结果
        Employee emp = employeeService.getOne(queryWrapper);

        //判断结果为空,没有查询到用户名
        if (emp == null) {
            return R.error("无此用户名");
        }
        //对比密码不正确
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //账号状态 禁用=0
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //用户状态正常。登录成功 id存入session
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /*
    员工退出登录
     */

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清楚session保存当前员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /*
    新增员工
     */
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request ,@RequestBody Employee employee) {
        log.info("新增员工信息 :{}",employee.toString());
        //设置密码,md5加密,默认密码123456,转化成数组
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //创建时间,当前系统时间
        employee.setCreateTime(LocalDateTime.now());
        //修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录用户的id
        Long id = (Long) request.getSession().getAttribute("employee");
        //创建用户
        employee.setCreateUser(id);
        //修改用户信息
        employee.setUpdateUser(id);
        //存入
        employeeService.save(employee);
        return R.success("新增用户信息");
    }


    /*
    分页查询
     */
    @GetMapping("/page")
    public R <Page> page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件 模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /*
    员工的禁用和启用
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //获取当前登录用户的id
        Long id = (Long) request.getSession().getAttribute("employee");
        //修改信息的用户id
        employee.setUpdateUser(id);
        //修改时间
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工修改成功");
    }
    /*
    根据id查询
    同修改员工一个接口
     */
    @GetMapping("/{id}")
    public R <Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee byId = employeeService.getById(id);
        if (byId !=null){
            return R.success(byId);
        }
        return R.error("查不无此用户信息");
    }




}




